#include "screenCommonComp.h"

lv_obj_t * BottomWidthTwentyPannelComp(lv_obj_t * parent,uint8_t size,comp_obj_prop_t * propList){
    lv_obj_t *BottomWidthTwentyPannel = lv_obj_create(parent);
    lv_obj_set_width(BottomWidthTwentyPannel, 360);
    lv_obj_set_height(BottomWidthTwentyPannel, 20);
    lv_obj_set_align(BottomWidthTwentyPannel, LV_ALIGN_BOTTOM_MID);
    lv_obj_set_flex_flow(BottomWidthTwentyPannel, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(BottomWidthTwentyPannel, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER,
                          LV_FLEX_ALIGN_CENTER);
    // lv_obj_add_flag(BottomWidthTwentyPannel, LV_OBJ_FLAG_HIDDEN);       /// Flags
    lv_obj_clear_flag(BottomWidthTwentyPannel, LV_OBJ_FLAG_SCROLLABLE); /// Flags
    lv_obj_set_style_radius(BottomWidthTwentyPannel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_color(BottomWidthTwentyPannel, lv_color_hex(0x000000),
                                  LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_opa(BottomWidthTwentyPannel, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(BottomWidthTwentyPannel, 1, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_side(BottomWidthTwentyPannel, LV_BORDER_SIDE_TOP, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_row(BottomWidthTwentyPannel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_column(BottomWidthTwentyPannel, 2, LV_PART_MAIN | LV_STATE_DEFAULT);

    for(uint8_t i=0;i<size;i++){
        comp_obj_prop_t *prop = &propList[i];
        if(prop->type == COMP_OBJ_TEXT){
            lv_obj_t *BottomWidthTwentyLabel = lv_label_create(BottomWidthTwentyPannel);
            lv_obj_set_width(BottomWidthTwentyLabel, LV_SIZE_CONTENT);  /// 1
            lv_obj_set_height(BottomWidthTwentyLabel, LV_SIZE_CONTENT); /// 1
            // lv_obj_set_x(BottomWidthTwentyLabel, -490);
            // lv_obj_set_y(BottomWidthTwentyLabel, -75);
            // lv_obj_set_align(BottomWidthTwentyLabel, LV_ALIGN_CENTER);
            lv_label_set_text(BottomWidthTwentyLabel, prop->text);
            lv_obj_set_style_text_color(BottomWidthTwentyLabel, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_text_opa(BottomWidthTwentyLabel, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_text_font(BottomWidthTwentyLabel, prop->font, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_pad_left(BottomWidthTwentyLabel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_pad_right(BottomWidthTwentyLabel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_pad_top(BottomWidthTwentyLabel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_pad_bottom(BottomWidthTwentyLabel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_pad_row(BottomWidthTwentyLabel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_pad_column(BottomWidthTwentyLabel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);

        }else if(prop->type == COMP_OBJ_IMAGE){
            lv_obj_t *BottomWidthTwentyImage = lv_img_create(BottomWidthTwentyPannel);
            lv_img_set_src(BottomWidthTwentyImage, prop->img_dsc);
            lv_obj_set_width(BottomWidthTwentyImage, LV_SIZE_CONTENT);  /// 16
            lv_obj_set_height(BottomWidthTwentyImage, LV_SIZE_CONTENT); /// 10
            // lv_obj_set_align(BottomWidthTwentyImage, LV_ALIGN_CENTER);
            lv_obj_add_flag(BottomWidthTwentyImage, LV_OBJ_FLAG_ADV_HITTEST);  /// Flags
            lv_obj_clear_flag(BottomWidthTwentyImage, LV_OBJ_FLAG_SCROLLABLE); /// Flags

        }
    }
    return BottomWidthTwentyPannel;
}

lv_obj_t * TopWidthTwentyFourPannelComp(lv_obj_t * parent,uint8_t size,comp_obj_prop_t * propList){
    lv_obj_t *BottomWidthTwentyPannel = lv_obj_create(parent);
    lv_obj_set_width(BottomWidthTwentyPannel, 360);
    lv_obj_set_height(BottomWidthTwentyPannel, 24);
    lv_obj_set_align(BottomWidthTwentyPannel, LV_ALIGN_BOTTOM_MID);
    lv_obj_set_flex_flow(BottomWidthTwentyPannel, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(BottomWidthTwentyPannel, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER,
                          LV_FLEX_ALIGN_CENTER);
    // lv_obj_add_flag(BottomWidthTwentyPannel, LV_OBJ_FLAG_HIDDEN);       /// Flags
    lv_obj_clear_flag(BottomWidthTwentyPannel, LV_OBJ_FLAG_SCROLLABLE); /// Flags
    lv_obj_set_style_radius(BottomWidthTwentyPannel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_color(BottomWidthTwentyPannel, lv_color_hex(0x000000),
                                  LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_opa(BottomWidthTwentyPannel, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(BottomWidthTwentyPannel, 1, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_side(BottomWidthTwentyPannel, LV_BORDER_SIDE_BOTTOM, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_row(BottomWidthTwentyPannel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_column(BottomWidthTwentyPannel, 4, LV_PART_MAIN | LV_STATE_DEFAULT);

    for(uint8_t i=0;i<size;i++){
        comp_obj_prop_t *prop = &propList[i];
        if(prop->type == COMP_OBJ_TEXT){
            lv_obj_t *BottomWidthTwentyLabel = lv_label_create(BottomWidthTwentyPannel);
            lv_obj_set_width(BottomWidthTwentyLabel, LV_SIZE_CONTENT);  /// 1
            lv_obj_set_height(BottomWidthTwentyLabel, LV_SIZE_CONTENT); /// 1
            // lv_obj_set_x(BottomWidthTwentyLabel, -490);
            // lv_obj_set_y(BottomWidthTwentyLabel, -75);
            // lv_obj_set_align(BottomWidthTwentyLabel, LV_ALIGN_CENTER);
            lv_label_set_text(BottomWidthTwentyLabel, prop->text);
            lv_obj_set_style_text_color(BottomWidthTwentyLabel, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_text_opa(BottomWidthTwentyLabel, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_text_font(BottomWidthTwentyLabel, prop->font, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_pad_left(BottomWidthTwentyLabel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_pad_right(BottomWidthTwentyLabel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_pad_top(BottomWidthTwentyLabel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_pad_bottom(BottomWidthTwentyLabel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_pad_row(BottomWidthTwentyLabel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_pad_column(BottomWidthTwentyLabel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);

        }else if(prop->type == COMP_OBJ_IMAGE){
            lv_obj_t *BottomWidthTwentyImage = lv_img_create(BottomWidthTwentyPannel);
            lv_img_set_src(BottomWidthTwentyImage, prop->img_dsc);
            lv_obj_set_width(BottomWidthTwentyImage, LV_SIZE_CONTENT);  /// 16
            lv_obj_set_height(BottomWidthTwentyImage, LV_SIZE_CONTENT); /// 10
            // lv_obj_set_align(BottomWidthTwentyImage, LV_ALIGN_CENTER);
            lv_obj_add_flag(BottomWidthTwentyImage, LV_OBJ_FLAG_ADV_HITTEST);  /// Flags
            lv_obj_clear_flag(BottomWidthTwentyImage, LV_OBJ_FLAG_SCROLLABLE); /// Flags

        }
    }
    return BottomWidthTwentyPannel;
}

lv_obj_t * ui_create_list_panel_base(lv_obj_t *parent,lv_coord_t width,lv_coord_t height,bool is_col,int size,comp_obj_prop_t propList[]){
    lv_obj_t *ui_list_panel = NULL;
    if(is_col){
        ui_list_panel = ui_create_column_panel(parent, width, height);
    }else{
        ui_list_panel = ui_create_row_panel(parent, width, height);
    }
    for (int i = 0; i < size; ++i) {
        comp_obj_prop_t *prop = &propList[i];
        if(prop->type == COMP_OBJ_TEXT){
            lv_obj_t *label = ui_create_default_label(ui_list_panel,prop->text,prop->font);
        }else if(prop->type == COMP_OBJ_IMAGE){
            lv_obj_t *image = ui_create_default_image_by_src(ui_list_panel, prop->img_dsc);
        }else if(prop->type == COMP_OBJ_PANEL){
            lv_obj_set_parent(prop->obj, ui_list_panel);
        }
    }
    return ui_list_panel;
}

lv_obj_t * ui_create_column_list_panel(lv_obj_t *parent,lv_coord_t width,lv_coord_t height,int size,comp_obj_prop_t propList[]){
    return ui_create_list_panel_base(parent, width, height, true, size, propList);
}

lv_obj_t *ui_create_default_column_list_panel(lv_obj_t *parent,int size,comp_obj_prop_t propList[]){
    return ui_create_column_list_panel(parent, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL), size, propList);
}

lv_obj_t * ui_create_row_list_panel(lv_obj_t *parent,lv_coord_t width,lv_coord_t height,int size,comp_obj_prop_t propList[]){
    return ui_create_list_panel_base(parent, width, height, false, size, propList);
}

lv_obj_t *ui_create_default_row_list_panel(lv_obj_t *parent,int size,comp_obj_prop_t propList[]){
    return ui_create_row_list_panel(parent, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL), size, propList);
}


// 长文本 按单词截断（自己加省略号）
// 需要配合这个使用：lv_label_set_long_mode(word_accent_container, LV_LABEL_LONG_CLIP); // 使用截断方式  自己绘制省略号
void ui_my_LABEL_LONG_DOT(const lv_obj_t * obj)
{
    lv_area_t txt_coords;
    lv_obj_get_content_coords(obj, &txt_coords);
    lv_coord_t max_w         = lv_area_get_width(&txt_coords);
    const lv_font_t * font   = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
    lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_MAIN);
    lv_coord_t letter_space = lv_obj_get_style_text_letter_space(obj, LV_PART_MAIN);

    lv_label_t * label = (lv_label_t *)obj;
    lv_point_t p;
    lv_coord_t y_overed;
    p.x = lv_area_get_width(&txt_coords) -
          (lv_font_get_glyph_width(font, '.', '.') + letter_space) *
          LV_LABEL_DOT_NUM; /*Shrink with dots*/
    p.y = lv_area_get_height(&txt_coords);
    y_overed = p.y %
               (lv_font_get_line_height(font) + line_space); /*Round down to the last line*/
    if(y_overed >= lv_font_get_line_height(font)) {
        p.y -= y_overed;
        p.y += lv_font_get_line_height(font);
    }
    else {
        p.y -= y_overed;
        p.y -= line_space;
    }
    printf("p.x:%d p.y:%d\n",p.x,p.y);
    uint32_t letter_id = lv_label_get_letter_on(obj, &p);
    printf("letter_id :%d \n",letter_id);
    /*Be sure there is space for the dots*/
    size_t txt_len = strlen(label->text);
    uint32_t byte_id     = _lv_txt_encoded_get_byte_id(label->text, letter_id);
    while(byte_id + LV_LABEL_DOT_NUM > txt_len) {
        _lv_txt_encoded_prev(label->text, &byte_id);
        letter_id--;
    }
    printf("txt_len : %d  letter_id :%d byte_id : %d\n",txt_len, letter_id,byte_id);

    /*Save letters under the dots and replace them with dots*/
    uint32_t byte_id_ori = byte_id;
    uint32_t i;
    uint8_t len = 0;
    for(i = 0; i <= LV_LABEL_DOT_NUM; i++) {
        len += _lv_txt_encoded_size(&label->text[byte_id]);
        _lv_txt_encoded_next(label->text, &byte_id);
        if(len > LV_LABEL_DOT_NUM || byte_id > txt_len) {
            break;
        }
    }
    printf("byte_id_ori : %d len : %d\n",byte_id_ori,len);
    for(i = 0; i < LV_LABEL_DOT_NUM; i++) {
        label->text[byte_id_ori + i] = '.';
    }
    label->text[byte_id_ori+LV_LABEL_DOT_NUM] = '\0';
    label->dot_end = letter_id + LV_LABEL_DOT_NUM;
    printf("out : %s",label->text);
}

/**
 * @brief 计算文字宽度
 * @param label
 * @param letter
 * @param letter_num
 * @return
 */
lv_coord_t ui_common_letter_width(lv_obj_t *label,char letter,int letter_num){
    lv_coord_t width = lv_font_get_glyph_width(lv_obj_get_style_text_font(label,0), letter, letter) + lv_obj_get_style_text_letter_space(label,0);
    LV_LOG_USER("width:%d",width);
    return width * letter_num;
}

lv_coord_t ui_common_get_label_text_width(lv_obj_t *label,char *text){
    lv_coord_t space = lv_obj_get_style_text_letter_space(label, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_coord_t label_width = lv_txt_get_width(text, strlen(text), lv_obj_get_style_text_font(label,0),space,LV_TEXT_FLAG_NONE);
    LV_LOG_USER("label_width:%d",label_width);
    return label_width;
}